import request from '@/utils/request';
import { fetchEventSource } from '@microsoft/fetch-event-source';
import { getToken } from '@/utils/auth';
import cryptStore from '@/store/modules/crypt';

// 后台管理子系统-数智融合-查询当前用户的会话
export function pageChatSession(params, data) {
  return request({
    url: '/web/lm/chat/session/page',
    method: 'post',
    params,
    data,
    hmacReq: true,
    encryptReq: true,
    decryptRes: true,
  });
}

// 后台管理子系统-数智融合-根据会话id查询历史
export function pageChatHistory(params, data) {
  return request({
    url: '/web/lm/chat/history/page',
    method: 'post',
    params,
    data,
    hmacReq: true,
    encryptReq: true,
    decryptRes: true,
  });
}

// 后台管理子系统-数智融合-大模型对话接口 sse类型
export async function chartCommon(data, callbackInfo) {
  const url = import.meta.env.VITE_APP_BASE_API + '/web/lm/chat/common';
  let hasNegotiate;
  while (!cryptStore().isCryptInitialized) {
    await cryptStore().negotiateKey(hasNegotiate);
    hasNegotiate = true;
  }
  return fetchEventSource(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'text/event-stream',
      Authorization: 'Bearer ' + getToken(),
    },
    body: cryptStore().encryptRequest(data), // JSON.stringify(data),
    ...callbackInfo,
  });
  // return request({

  //   method: 'post',
  //   params,
  //   data,
  //   hmacReq: true,
  //   encryptReq: true,
  //   decryptRes: true,
  // });
}
